/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.zjhcsoft.komm;

import java.io.Serializable;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.JoinColumns;
import javax.persistence.JoinColumns;
import javax.persistence.JoinColumns;
import javax.persistence.JoinColumns;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.Version;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Scope;

/**
 *
 * @author chenlin
 */
@Entity
@Table(name = "user")
@Scope(ScopeType.SESSION)
public class User implements Serializable {

    private Long id;
    private Integer version;
    private String username;
    private String password;
    private String realName;
    private String department;
    private String phone;
    private String email;
    private Set<Role> roles;

    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Version
    public Integer getVersion() {
        return version;
    }

    private void setVersion(Integer version) {
        this.version = version;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @ManyToMany
    @JoinTable(name = "user_role",
    joinColumns = @JoinColumn(name = "user_id"),
    inverseJoinColumns = @JoinColumn(name = "role_id"))
    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }

    @Transient
    public boolean hasPermission(String target, String action) {
        if (getRoles() != null && getRoles().size() > 0) {
            for (Role role : getRoles()) {
                if (role.getPermissions() != null && role.getPermissions().size() > 0) {
                    for (Permission permission : role.getPermissions()) {
                        if (permission.getTarget().equalsIgnoreCase(target) &&
                                permission.getAction().equalsIgnoreCase(action)) {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }
}
